package top.superflow.service.view.utils

import top.superflow.constant.ViewConst
import top.superflow.lang.StringUtils
import top.superflow.service.view.dto.{ActionDef, CellType, ColumnDef}
import top.superflow.service.view.inf.WithAction

object ActionUtils {

  private val PREDEFINED_CELL_RENDER = Map[String, String](("metadata", "propertyRender"),("routes","gridRender"))

  private val DEFAULT_ACTION_CELL_RENDER = "dropMenu"

  private val DEFAULT_ACTION_CELL_WIDTH = 100

  final val DEFAULT_PINNED_POSITION = "right"

  private val PREDEFINED_ORDER_LAST_NUM = 10000

  def setup(columnDef : ColumnDef){
    if(columnDef == null){
      return
    }

    val predefinedCellRender = PREDEFINED_CELL_RENDER.get(columnDef.getHeaderName)

    columnDef.setCellType(CellType.ACTION)
    columnDef.setDisplayOrder(PREDEFINED_ORDER_LAST_NUM)

//    if(!predefinedCellRender.isEmpty){
//      columnDef.setCellRenderer(predefinedCellRender)
//    }else{
//      columnDef.setCellRenderer(DEFAULT_CELL_RENDER)
//    }
    columnDef.setCellRenderer(DEFAULT_ACTION_CELL_RENDER)
    columnDef.setWidth(DEFAULT_ACTION_CELL_WIDTH)

    setActions(columnDef)
  }

  private def setActions(columnDef : ColumnDef): Unit ={
    if(StringUtils.containsIgnoreCase(columnDef.getHeaderName, ViewConst.COLUMN_NAME_OPERATION)){
      columnDef.setActions(WithAction.ACTION_ALL)
    }else{
      columnDef.setActions(WithAction.ACTION_VIEW_UPDATE)
    }

  }
}
